<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: kane <chengjin005@163.com> 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\Db;

/**
 * Class NavController 导航类别管理控制器
 * @package app\admin\controller
 */
class CompanyController extends AdminBaseController
{

	/**
	 * 公司查询页面（弹框）
	 */
	public function find()
	{
		return $this->fetch();

	}

	/**
	 * 公司查询页面结果
	 */
	public function find_result()
	{
		$data = $this->request->param();
		$company_name = $data['sid'];
		$result = Db::name('company')->where("company_name like '%$company_name%'")->select()->toArray();
		$str = '';
		if(!$result)
		{	
			$result[0]['company_name'] = '';
			$result[0]['id'] = 0;
		}
		foreach ($result as $k => $v) {
        	$datas[] = "{'keywords':'" . $v['company_name'] . "','company_id':'" . $v['id'] . "'}";
        }
        $str = '[' . implode(',', $datas) . ']';
        echo $str;
        die;
	}
	/**
	 * 公司列表
	 */
	public function index()
	{
		$data = $this->request->param();
		//页码
		$now_page = isset($data['page'])?$data['page']:'1';
		//搜索名称
		$company_name = isset($data['company_name'])?$data['company_name']:'';;
		//分页数据
		$result = Db::name('company')->where("company_name like '%$company_name%' ")->paginate(10,false,['query'=>['company_name'=>$company_name]]);
		//总条数
		$sum_result_count = Db::name('company')->where("company_name like '%$company_name%' ")->count();

		$this->assign('result',$result->items());
		$this->assign('page',$result->render());
		$this->assign('now_page',$now_page);
		$this->assign('sum_result_count',$sum_result_count);
		$this->assign('company_name',$company_name);
		return $this->fetch();
	}

	/**
	 * 公司添加页面
	 */
	public function add()
	{
		return $this->fetch();
	}

	/**
	 * 公司添加
	 */
	public function addPost()
	{
		$post = $this->request->param();
		$data = $post['post'];
		$data['add_time']    = time();
		$data['update_time'] = $data['add_time'];
		$data['company_desc'] = isset($data['company_desc'])?html_entity_decode(htmlspecialchars_decode($data['company_desc'])):'';
		Db::name('company')->insert($data);
		$this->success("添加成功！", url("company/index"));
	}
	/**
	 * 公司修改页面
	 */
	public function edit()
	{	
		$data =  $this->request->param();
		$id = $data['id'];
		$result = Db::name('company')->where(['id'=>$id])->find();
		$this->assign('result',$result);
		return $this->fetch();
	}

	/**
	 * 公司修改
	 */
	public function editPost()
	{
		$post = $this->request->param();
		$data = $post['post'];
		$data['update_time']  = time();
 		$data['company_desc'] = isset($data['company_desc'])?html_entity_decode(htmlspecialchars_decode($data['company_desc'])):'';
		$id   = $post['id'];
		Db::name('company')->where(['id'=>$id])->update($data);
		$this->success('修改成功', url("company/index"));
	}

	/**
	 * 公司详情
	 */
	public function detail()
	{
		$id =  $this->request->param('id');
		$status = $this->request->param('status');
		//公司基本信息
		$result = Db::name('company')->where(['id'=>$id])->find();
		//客户信息
		$customer_result = Db::name('customer')->where(['company_id'=>$id])->select()->toArray();
		//返回开启状态
		$status = isset($status)?$status:1; //1显示 2 隐藏
		//项目信息
		$project_result = Db::name('project')->where(['company_id'=>$result['id']])->select()->toArray();
		$this->assign('project_result',$project_result);
		$this->assign('customer_result',$customer_result);
		$this->assign('result',$result);
		$this->assign('status',$status);
		return $this->fetch();
	}

	/**
	 * 三级联动页面
	 */
	public function address()
	{
		$result = Db::name('region')->where(['parent_id'=>0])->select()->toArray();
		$this->assign('result',$result);
		return $this->fetch();
	}

	/**
	 * 三级联动查询
	 */
	public function address_select()
	{
		$id =  $this->request->param('id');
		$result = Db::name('region')->where(['parent_id'=>$id])->select()->toArray();
		ajaxReturn(['data'=>$result]);
	}

	/**
	 * 地区拼接名称
	 */
	public function address_name_select()
	{
		$sheng =  $this->request->param('sheng');
		$shi =  $this->request->param('shi');
		$qu =  $this->request->param('qu');
		$result =  Db::name('region')->where(['id'=>['in',[$sheng,$shi,$qu]]])->select()->toArray();
		$str = '';
		foreach($result as $k=>$v)
		{
			$str .= $v['name'];
		}
		ajaxReturn(['data'=>$str]);
	}
}